namespace TSL.DAL
{
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using System.Text;
    using TSL.Model;

    public class Notice
    {
        public void AddNotice(TSL.Model.Notice model)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("TSL_Notice_ADD");
            db.AddInParameter(dbCommand, "classid", DbType.AnsiString, model.classid);
            db.AddInParameter(dbCommand, "textname", DbType.AnsiString, model.textname);
            db.AddInParameter(dbCommand, "addtime", DbType.AnsiString, model.addtime);
            db.AddInParameter(dbCommand, "content", DbType.AnsiString, model.content);
            db.ExecuteNonQuery(dbCommand);
        }

        public void Delete(string ID)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("TSL_Notice_Delete");
            db.AddInParameter(dbCommand, "ID", DbType.AnsiString, ID);
            db.ExecuteNonQuery(dbCommand);
        }

        public void DeleteNotice(string ID)
        {
            List<string> strList = new List<string>();
            strList.Add("delete from Notice where ID = '" + ID + "'");
            DbHelperSQL.ExecuteSqlTran(strList);
        }

        public bool Exists(string name)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Notice");
            strSql.Append(" where textname=@name ");
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@name", SqlDbType.VarChar, 50) };
            parameters[0].Value = name;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        public List<TSL.Model.Notice> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from V_Notice ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append("order by id desc");
            List<TSL.Model.Notice> list = new List<TSL.Model.Notice>();
            using (IDataReader dataReader = DatabaseFactory.CreateDatabase().ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(this.ReaderBind(dataReader));
                }
            }
            return list;
        }

        public DataSet GetListFromView(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from V_Notice");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by id desc");
            return DatabaseFactory.CreateDatabase().ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        public TSL.Model.Notice GetModel(string ID)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("SYS_Notice_GetModelByID");
            db.AddInParameter(dbCommand, "ID", DbType.AnsiString, ID);
            TSL.Model.Notice model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = this.ReaderBind(dataReader);
                }
            }
            return model;
        }

        public TSL.Model.Notice GetModel(string name, string content)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("SYS_Notice_GetModel");
            db.AddInParameter(dbCommand, "username", DbType.AnsiString, name);
            db.AddInParameter(dbCommand, "content", DbType.AnsiString, content);
            TSL.Model.Notice model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = this.ReaderBind(dataReader);
                }
            }
            return model;
        }

        public DataSet GetNews(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 5 * from V_GetNotice");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by id desc ");
            return DatabaseFactory.CreateDatabase().ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        public DataSet GetNotice(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 5 * from V_GetNotice");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by id desc ");
            return DatabaseFactory.CreateDatabase().ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        public TSL.Model.Notice ReaderBind(IDataReader dataReader)
        {
            TSL.Model.Notice model = new TSL.Model.Notice();
            model.id = dataReader["id"].ToString();
            model.classid = dataReader["classid"].ToString();
            model.textname = dataReader["textname"].ToString();
            model.addtime = dataReader["addtime"].ToString();
            model.content = dataReader["content"].ToString();
            return model;
        }

        public void Update(TSL.Model.Notice model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update notice set ");
            strSql.Append("classid=@classid,");
            strSql.Append("textname=@textname,");
            strSql.Append("addtime=@addtime,");
            strSql.Append("content=@content");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@classid", SqlDbType.VarChar,50),
					new SqlParameter("@textname", SqlDbType.VarChar,255),
					new SqlParameter("@addtime", SqlDbType.DateTime),
					new SqlParameter("@content", SqlDbType.NVarChar),
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.classid;
            parameters[1].Value = model.textname;
            parameters[2].Value = model.addtime;
            parameters[3].Value = model.content;
            parameters[4].Value = model.id;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
    }
}

